using Godot;
[GlobalClass]
public partial class StateMachine : Node
{
    private int _currentState = -1;
    public int currentState
    {
        get { return _currentState; }
        set
        {
            if (Owner is StateMachineOwner owner)
                owner.TransitionSate(currentState, value);
            _currentState = value;
        }
    }

    public override void _Ready()
    {
        currentState = 0;
    }

    public override void _PhysicsProcess(double delta)
    {
        if (Owner is StateMachineOwner owner)
        {
            while (true)
            {
                var next = owner.GetNextState(currentState);
                if (currentState == next) break;
                currentState = next;
            }
            owner.TickPhysics(currentState,delta);
        }
    }
}

public interface StateMachineOwner
{
    public void TransitionSate(int from, int to); //转换状态时
    public int GetNextState(int currentSate); //判断是否可以进入下一个状态
    public void TickPhysics(int currentSate, double delta); //每帧根据该状态处理行为

}